package com.swz.dcrm.ui.mine;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.PersonalInfo;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.StaffViewModel;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class StaffDetailFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_post)
    TextView tvPost;

    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_group)
    TextView tvGroup;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.tv_right)
    TextView tvRight;
    private PersonalInfo personalInfo;
    @Inject
    StaffViewModel staffViewModel;
    public static StaffDetailFragment newInstance() {

        return new StaffDetailFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
        long id=Long.parseLong(StaffDetailFragmentArgs.fromBundle(getArguments()).getId());
        staffViewModel.getStaff(id)
                .observe(this, personalInfoBaseResponse -> {
                    if(personalInfoBaseResponse.isSuccess()&&personalInfoBaseResponse.getData().getId()==id){
                        personalInfo=personalInfoBaseResponse.getData();
                        tvName.setText(personalInfoBaseResponse.getData().getName());
                        tvGroup.setText(personalInfoBaseResponse.getData().getGroupName());
                        tvPhone.setText(personalInfoBaseResponse.getData().getPhone());

                        tvSex.setText(personalInfoBaseResponse.getData().getSexDesc());
                        tvPost.setText(personalInfoBaseResponse.getData().getRole().getRoleGroup());
                    }
                });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_staff_detail;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.staff_detail_title));
        tvRight.setText(getString(R.string.staff_detail_edit));
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }

    @OnClick(R.id.tv_right)
    public void edit() {
        StaffInformationFragmentArgs.Builder builder=new StaffInformationFragmentArgs.Builder();
        builder.setPersonalInfo(new Gson().toJson(personalInfo));
        go(null,R.id.action_staffDetailFragment_to_staffInformationFragment,builder.build().toBundle());
    }
}
